<template>
  <div>
    <router-link class="back-icon" to="/" v-show="!showToggle">
      <span class="iconfont">&#xe679;</span>
    </router-link>
    <div class="detail-header" v-show="showToggle" :style="headerStyle">
      <router-link class="header-back-icon" to="/">
        <span class="iconfont">&#xe679;</span>
      </router-link>
      <h3 class="header-title">{{ sightName }}</h3>
    </div>
  </div>
</template>

<script>
export default {
  name: 'DetailHeader',
  props: {
    sightName: String
  },
  data () {
    return {
      showToggle: false,
      headerStyle: {
        opacity: 0
      }
    }
  },
  methods: {
    scrollEvent () { // 顶部导航栏渐隐渐现效果
      const top = document.documentElement.scrollTop
      if (top > 10) {
        let opacity = top / 140
        opacity = opacity > 1 ? 1 : opacity
        this.headerStyle.opacity = opacity
        this.showToggle = true
      } else {
        this.showToggle = false
      }
    }
  },
  mounted () {
    console.log('1111')
  },
  activated () { // 进入该路由（路由激活）时触发
    window.addEventListener('scroll', this.scrollEvent)
  },
  deactivated () { // 离开当前页面（路由移除）时触发
    window.removeEventListener('scroll', this.scrollEvent)
  }
}
</script>

<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  @import '~styles/mixins.styl'
  .back-icon
    position: absolute
    z-index: 2
    top: .2rem
    left: .2rem
    width: .6rem
    height: .6rem
    line-height: .6rem
    border-radius: .3rem
    background-color: rgba(0, 0, 0, 0.7)
    color: #fff
    text-align: center
    opacity: 1
  .detail-header
    position: fixed
    z-index: 3
    top: 0
    left: 0
    right: 0
    height: $headerBsrHeight
    background-color: $bgColor
    line-height: $headerBsrHeight
    font-size: .32rem
    .header-back-icon
      position: absolute
      left: .2rem
      color: #fff
      span
        font-size: .36rem
    .header-title
      width: 70%
      color: #fff
      text-align: center
      margin-left: 15%
      ellipsis()
</style>
